package org.openotdr.server;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.openotdr.server.otdr.Sr4731Parser;

import com.miyake.rest.ServiceType;
import com.miyake.rest.StreamToByteConverter;
import com.miyake.rest.WebServiceProperty;

public class FileServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        WebServiceProperty prop = new PropertyBuilder() {

            @Override
            protected String getDescription() {
                return "Open source SR4731 OTDR file parser project";
            }

            @Override
            protected String getServiceName() {
                return "SR4731 File Parser";
            }

            @Override
            protected String getServiceUrl() {
                return "http://openotdr.appspot.com/fileServlet";
            }

            @Override
            protected ServiceType getServiceType() {
                return ServiceType.FileParser;
            }

            @Override
            protected String getVersion() {
                return "1.00";
            }
            
        }.getProperty();

        resp.getWriter().print(prop.toXML());
        resp.getWriter().flush();
        resp.getWriter().close();
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        
        if (isBrowser(req)) {
            try {
                ServletFileUpload upload = new ServletFileUpload();  
                int sizeMax = -1;
                upload.setSizeMax(sizeMax);
                upload.setFileSizeMax(sizeMax);
                
                FileItemIterator iterator = upload.getItemIterator(req);       

                resp.setContentType("text/html");   
                while (iterator.hasNext()) {           
                    FileItemStream item = iterator.next();  
                    resp.getWriter().print(new CsvParser(new Sr4731Parser(new StreamToByteConverter(item.openStream()).get())).toCsv());
                    //resp.getWriter().print(getXml(new BufferedInputStream(item.openStream())));
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        else if (isApplication(req)){
            try {
                resp.getWriter().print(getXml(req.getInputStream())); 
            } catch (IOException e) {
                e.printStackTrace(resp.getWriter());
            }
        }

        resp.getWriter().flush();
        resp.getWriter().close();
    }

    private boolean isApplication(HttpServletRequest req) {
        return req.getContentType().equals("application/octet-stream");
    }

    private boolean isBrowser(HttpServletRequest req) {
        return req.getContentType().contains("multipart/form-data");
    }

    private String getXml(InputStream bis) {
        String xml = new WebServicePropertyConverter(new Sr4731Parser(new StreamToByteConverter(bis).get())).toXML();
        return xml;
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}
